#!/usr/bin/ruby -w

# Simple boilerplate generator for Evergreen.

# Test with:
# evergreen-boilerplate-generator /usr/enh/projects/blah/src/SomeClass.h /usr/enh/projects/blah/src/org/jessies/talc/Talc.java

def cpp(path)
    # Turn "/usr/enh/Projects/blah/src/SomeClass.h" into "SomeClass.h".
    name = File.basename(path)
    # Turn "SomeClass.h" into "SOME_CLASS_H".
    safe_name = name.gsub(/([a-z])([A-Z])/, "\\1_\\2").upcase().gsub(/[^A-Za-z0-9_]/, "_")

    macro_name = "#{safe_name}_included"

    puts("#ifndef #{macro_name}")
    puts("#define #{macro_name}")
    puts()
    puts()
    puts()
    puts("#endif")
end

def java(path)
    # Extract the class and package names.
    elements = path.split(File::SEPARATOR)
    class_name = elements.pop().sub(/\.java$/, "")
    while elements.size() > 0 && elements.first() !~ /^(com|net|org)$/
        elements.shift()
    end
    package = elements.join(".")
    
    if package.size() > 0
        puts("package #{package};")
        puts()
    end
    # Does this look like a JUnit test?
    if path.include?(File::SEPARATOR + "test" + File::SEPARATOR) && class_name =~ /Test$/
        puts("import org.junit.*;")
        puts("import static org.junit.Assert.*;")
        puts()
    end
    puts("public class #{class_name} {")
    puts("}")
end

def emit_boilerplate(path)
    if path =~ /\.h$/
        cpp(path)
    elsif path =~ /\.java$/
        java(path)
    end
end

ARGV.each() {
    |path|
    emit_boilerplate(path)
}
